﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebMVC.Controllers
{
    public class KhuVucController : Controller
    {
        databaseDataContext db = new databaseDataContext();
        //
        // GET: /KhuVuc/
        [Authorize(Roles = "0")]
        public ActionResult Index()
        {
            ViewData["Items"] = from k in db.KhuVucs
                                select k;
            return View();
        }

        //
        // GET: /KhuVuc/Details/5
        [Authorize(Roles = "0")]
        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /KhuVuc/Create
        [Authorize(Roles = "0")]
        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /KhuVuc/Create
        [Authorize(Roles = "0")]
        [HttpPost]
        public ActionResult Create(KhuVuc khuvuc)
        {
            try
            {
                // TODO: Add insert logic here
                khuvuc.Ngay_cap_nhat_cuoi = DateTime.Now;
                db.KhuVucs.InsertOnSubmit(khuvuc);
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /KhuVuc/Edit/5
        [Authorize(Roles = "0")]
        public ActionResult Edit(int id)
        {
            KhuVuc model = (from m in db.KhuVucs
                             where m.ID == id
                             select m).SingleOrDefault();
            
            return View(model);
        }

        //
        // POST: /KhuVuc/Edit/5
        [Authorize(Roles = "0")]
        [HttpPost]
        public ActionResult Edit(int id, KhuVuc khuvuc)
        {
            KhuVuc model = (from m in db.KhuVucs
                            where m.ID == id
                            select m).SingleOrDefault();
            try
            {
                // TODO: Add update logic here
                model.Ten = khuvuc.Ten;
                model.Ghi_chu = khuvuc.Ghi_chu;
                model.Ngay_cap_nhat_cuoi = DateTime.Now;
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View(model);
            }
        }

        //
        // GET: /KhuVuc/Delete/5
        [Authorize(Roles = "0")]
        public ActionResult Delete(int id)
        {
            var model = (from h in db.KhuVucs
                         where h.ID == id
                         select h).Take(1).SingleOrDefault();
            db.KhuVucs.DeleteOnSubmit(model);
            db.SubmitChanges();
            return RedirectToAction("Index");
        }

        //
        // POST: /KhuVuc/Delete/5
        [Authorize(Roles = "0")]
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
